<script>
  import { toasts, removeToast } from '../stores/toast';
  import { IconCheckCircle, IconXCircle, IconAlertTriangle, IconInfo, IconClose } from '../utils/icons';

  const iconMap = {
    success: IconCheckCircle,
    error: IconXCircle,
    warning: IconAlertTriangle,
    info: IconInfo,
  };

  const colorMap = {
    success: '#22c55e',
    error: '#ef4444',
    warning: '#f59e0b',
    info: '#3b82f6',
  };
</script>

<div class="toast-container">
  {#each $toasts as toast (toast.id)}
    <div class="toast toast-{toast.type}" style="--toast-color: {colorMap[toast.type]}">
      <svelte:component this={iconMap[toast.type]} size={20} />
      <span class="message">{toast.message}</span>
      <button class="close" on:click={() => removeToast(toast.id)} aria-label="Close notification">
        <IconClose size={16} />
      </button>
    </div>
  {/each}
</div>

<style>
  .toast-container {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-width: 400px;
  }

  .toast {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    border-radius: 4px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 0 2px 4px rgba(0, 0, 0, 0.06);
    background: white;
    border-left: 4px solid var(--toast-color);
    animation: slideIn 0.3s ease-out;
    min-width: 300px;
  }

  .toast :global(svg) {
    color: var(--toast-color);
    flex-shrink: 0;
  }

  .message {
    flex: 1;
    font-size: 0.875rem;
    line-height: 1.25rem;
    color: #374151;
  }

  .close {
    background: none;
    border: none;
    cursor: pointer;
    padding: 0.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #9ca3af;
    transition: color 0.2s;
    flex-shrink: 0;
  }

  .close:hover {
    color: #4b5563;
  }

  .close:focus {
    outline: 2px solid var(--toast-color);
    outline-offset: 2px;
    border-radius: 2px;
  }

  @keyframes slideIn {
    from {
      transform: translateX(100%);
      opacity: 0;
    }
    to {
      transform: translateX(0);
      opacity: 1;
    }
  }

  @media (max-width: 640px) {
    .toast-container {
      left: 1rem;
      right: 1rem;
      max-width: none;
    }

    .toast {
      min-width: auto;
    }
  }
</style>
